package UnitTest;

/**
 * @Auther: Administrator
 * @Date: 2020/1/1/001 21 55
 * @Description:
 */
public class BinarySearch {

    public static void main(String[] args) {

        int[] arrays = {2, 4, 5, 7, 8, 9, 12};

        int index = binarySearch(arrays, 8, 0, arrays.length);

        System.out.println("在index = " + index + "处命中");

    }

    private static int binarySearch(int[] arrays, int e, int lo, int hi) {
        while (lo < hi) {
            //int mi = (lo + hi) /2 ;
            int mi = lo + (hi - lo) / 2;
            if (e < arrays[mi]) {
                hi = mi;
            } else if (e > arrays[mi]) {
                lo = mi + 1;
            } else return mi;
        }
        return -1;
    }

}
